package org.gp.Interfaz;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.gp.tringes.GestorIGAnadirPerro;
import org.gp.tringes.GestorIGInicio;

import java.awt.GridLayout;
import java.sql.SQLException;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

public class IGAnadirPerro extends JDialog {
	
	private String numFederado;
	
	private JPanel panel;
	private JLabel lblNumId;
	private JTextField txtNumId;
	private JLabel lblNombre;
	private JTextField txtNombre;
	private JLabel lblRaza;
	private JTextField txtRaza;
	private JLabel lblSexo;
	private JTextField txtSexo;
	
	private JPanel panel_1;
	private JButton btnAnadir;
	private JButton btnCancelar;
	
	private JPanel panel_2;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IGAnadirPerro dialog = new IGAnadirPerro("");
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
					dialog.setModal(true);
					dialog.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public IGAnadirPerro(String pNumFederado) {
		numFederado = pNumFederado;
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
		//ID-46
		setTitle(GestorIGInicio.getGestorIGInicio().getTexto(46));
		setBounds(100, 100, 400, 250);
		
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.CENTER);
		getContentPane().add(getPanel_1(), BorderLayout.SOUTH);
		getContentPane().add(getPanel_2(), BorderLayout.NORTH);
		
	}
	
	private JPanel getPanel(){
		if(panel == null){
			panel = new JPanel();
			panel.setLayout(new GridLayout(4, 2, 0, 15));
			panel.add(getLblNumId());
			panel.add(getTxtNumId());
			panel.add(getLblNombre());		
			panel.add(getTxtNombre());
			panel.add(getLblRaza());
			panel.add(getTxtRaza());
			panel.add(getLblSexo());
			panel.add(getTxtSexo());
		}
		return panel;
	}
	private JLabel getLblNumId(){
		if(lblNumId == null){
			//ID-47
			lblNumId = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(47));
			lblNumId.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNumId;
	}
	private JTextField getTxtNumId(){
		if(txtNumId == null){
			txtNumId = new JTextField();
			txtNumId.setColumns(10);
		}
		return txtNumId;
	}
	private JLabel getLblNombre(){
		if(lblNombre == null){
			////ID-48
			lblNombre = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(48));
			lblNombre.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNombre;
	}
	private JTextField getTxtNombre(){
		if(txtNombre == null){
			txtNombre = new JTextField();
			txtNombre.setColumns(10);
		}
		return txtNombre;
	}
	private JLabel getLblRaza(){
		if(lblRaza == null){
			//ID-49
			lblRaza = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(49));
			lblRaza.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblRaza;
	}
	private JTextField getTxtRaza(){
		if(txtRaza == null){
			txtRaza = new JTextField();
			txtRaza.setColumns(10);
		}
		return txtRaza;
	}
	private JLabel getLblSexo(){
		if(lblSexo == null){
			//ID-50
			lblSexo = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(50));
			lblSexo.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblSexo;
	}
	private JTextField getTxtSexo(){
		if(txtSexo == null){
			txtSexo = new JTextField();
			txtSexo.setColumns(10);
		}
		return txtSexo;
	}
	
	private JPanel getPanel_1(){
		if(panel_1 == null){
			panel_1 = new JPanel();
			panel_1.add(getBtnAnadir()); 
			panel_1.add(getBtnCancelar());
		}
		return panel_1;
	}
	private JButton getBtnAnadir(){
		if(btnAnadir == null){
			//ID-29
			btnAnadir = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(29));
			btnAnadir.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					try{
						Boolean longNumId = GestorIGAnadirPerro.getGestorIGAnadirPerro().comprobarLongitudNumId(txtNumId.getText());
						if(longNumId){
							Boolean longNom = GestorIGAnadirPerro.getGestorIGAnadirPerro().comprobarLongitudNombre(txtNombre.getText());
							if(longNom){
								Boolean longRaza = GestorIGAnadirPerro.getGestorIGAnadirPerro().comprobarLongitudRaza(txtRaza.getText());
								if(longRaza){
									Boolean longSexo = GestorIGAnadirPerro.getGestorIGAnadirPerro().comprobarLongitudSexo(txtSexo.getText());
									if(longSexo){
										Boolean existePerro = GestorIGAnadirPerro.getGestorIGAnadirPerro().existePerro(txtNumId.getText());
										if(!existePerro){
											GestorIGAnadirPerro.getGestorIGAnadirPerro().insertarPerro(txtNumId.getText(), txtNombre.getText(), txtRaza.getText(), txtSexo.getText());
											GestorIGAnadirPerro.getGestorIGAnadirPerro().asignarPerroADueno(numFederado, txtNumId.getText(), GestorIGInicio.getGestorIGInicio().getFechaActual());
											//ID-21 y 6
											JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(21), GestorIGInicio.getGestorIGInicio().getTexto(6), JOptionPane.INFORMATION_MESSAGE);
											dispose();
										}else{
											//ID-51 y 4
											JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(51), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
										}
									}else{
										//ID-52 y 4
										JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(52), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
									}
								}else{
									//ID-53 y 4
									JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(53), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
								}
							}else{
								//ID-54 y 4
								JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(54), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
							}
						}else{
							//ID-55 y 4
							JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(55),GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
						}
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
		}
		return btnAnadir;
	}
	private JButton getBtnCancelar(){
		if(btnCancelar == null){
			//ID-8
			btnCancelar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(8));
			btnCancelar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					dispose();
				}
			});
		}
		return btnCancelar;
	}
	
	private JPanel getPanel_2(){
		if(panel_2 == null){
			panel_2 = new JPanel();
		}
		return panel_2;
	}
}
